import "package:flutter/material.dart";
import 'package:login_demo/widget/button.dart';
import 'package:login_demo/widget/smsCodeItem.dart';
import 'package:oktoast/oktoast.dart';

import 'app_theme.dart';
import 'login.dart';
// desc: 验证码
class SmsCode extends StatefulWidget {
  const SmsCode({Key key}) : super(key: key);

  @override
  _SmsCodeState createState() => _SmsCodeState();
}

class _SmsCodeState extends State<SmsCode> {

  TextEditingController smsController = new TextEditingController();

  @override
  void initState() {
    Future.delayed(Duration(seconds: 2),(){
      showToast(
        '收到验证码：1234',
        duration: Duration(seconds: 2)
      );
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.only(left: 15, right: 15),
        child: Column(
          children: [
            SizedBox(height: 40),
            Row(
              children: [
                GestureDetector(
                  onTap: (){
                    Navigator.pop(context);
                  },
                  child: Icon(Icons.arrow_back_ios_rounded),
                ),
              ],
            ),
            SizedBox(height: 40),
            Text('输入验证码', style: TextStyle(
                fontSize: 16,
                color: AppTheme.textBlack,
                fontWeight: FontWeight.bold
            )),
            SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: _buildSmsCode()
            ),
            SizedBox(height: 40),
            Button(
              text: '确定',
              onTap: (){
                _sure();
              },
            )
          ],
        ),
      ),
    );
  }

  _sure(){
    showToast('注册成功');
    Navigator.push<dynamic>(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => Login()
        )
    );
  }
  List<FocusNode> focusNodes = [];
  List<TextEditingController> controllers = [];
  List<Widget> _buildSmsCode(){
    List<Widget> smsCodeItems = [];
    for(int i=0; i< 4; i++){
      FocusNode focusNode = new FocusNode();
      TextEditingController controller = new TextEditingController();
      focusNodes.add(focusNode);
      controllers.add(controller);
      smsCodeItems.add(
          SmsCodeItem(
            focusNode: focusNode,
            textEditingController: controller,
            onChange: (value){
              print(value);
              if(i != 3){
                if(value.length == 1){
                  print('触发');
                  FocusScope.of(context).requestFocus(focusNodes[i+1]);
                }
              }
            },
          )
      );
    }
    return smsCodeItems;
  }
}